package com.ilearn.iodata.iolingo.config;


import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.ilearn.iodata.iolingo.properties.DruidDataSourceProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.sql.SQLException;

@Configuration
@EnableTransactionManagement
public class DruidDataSourceConfig {
    private static Logger logger = LoggerFactory.getLogger(DruidDataSourceConfig.class);

    @Autowired
    private DruidDataSourceProperties druidDataSourceProperties;

    public static String DRIVER_CLASSNAME;

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        ServletRegistrationBean reg = new ServletRegistrationBean();
        reg.setServlet(new StatViewServlet());
        //reg.setAsyncSupported(true);
        reg.addUrlMappings("/druid/*");
        /*reg.addInitParameter("allow", "127.0.0.1");
        reg.addInitParameter("deny", "/deny");*/
        reg.addInitParameter("loginUsername", "odata");
        reg.addInitParameter("loginPassword", "odata");

        logger.info(" druid console manager init:{}", reg);
        return reg;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico, /druid/*");
        logger.info(" druid filter register : {}", filterRegistrationBean);
        return filterRegistrationBean;
    }

    @Bean
    public DataSource dataSource() throws SQLException {
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(druidDataSourceProperties.getDriverClassName());
        DRIVER_CLASSNAME = druidDataSourceProperties.getDriverClassName();
        ds.setUrl(druidDataSourceProperties.getUrl());
        ds.setUsername(druidDataSourceProperties.getUsername());
        ds.setPassword(druidDataSourceProperties.getPassword());
        ds.setInitialSize(druidDataSourceProperties.getInitialSize());
        ds.setMinIdle(druidDataSourceProperties.getMinIdle());
        ds.setMaxActive(druidDataSourceProperties.getMaxActive());
        ds.setTimeBetweenEvictionRunsMillis(druidDataSourceProperties.getTimeBetweenEvictionRunsMillis());
        ds.setMinEvictableIdleTimeMillis(druidDataSourceProperties.getMinEvictableIdleTimeMillis());
        ds.setValidationQuery(druidDataSourceProperties.getValidationQuery());
        ds.setTestWhileIdle(druidDataSourceProperties.isTestWhileIdle());
        ds.setTestOnBorrow(druidDataSourceProperties.isTestOnBorrow());
        ds.setTestOnReturn(druidDataSourceProperties.isTestOnReturn());
        ds.setPoolPreparedStatements(druidDataSourceProperties.isPoolPreparedStatements());
        ds.setMaxPoolPreparedStatementPerConnectionSize(druidDataSourceProperties.getMaxPoolPreparedStatementPerConnectionSize());
        ds.setFilters(druidDataSourceProperties.getFilters());
        ds.setConnectProperties(druidDataSourceProperties.getConnectionProperties());
        //proxyFilters ====> 有问题
        /*WallFilter wallFilter = new WallFilter();
        WallConfig wallConfig = new WallConfig();
        wallConfig.setMultiStatementAllow(true);
        wallFilter.setConfig(wallConfig);
        List<Filter> wallFilterList = new ArrayList<Filter>();
        wallFilterList.add(wallFilter);
        ds.setProxyFilters(wallFilterList);*/
        logger.info(" druid datasource config : {}", ds);
        return ds;
    }

    @Bean
    public PlatformTransactionManager transactionManager() throws SQLException {
        DataSourceTransactionManager txManager = new DataSourceTransactionManager();
        txManager.setDataSource(dataSource());
        return txManager;
    }
}
